// pages/user/user.js
var app = getApp()
Page( {
  data: {
    applyForStatus: 0,//申请状态
    applyForRemark: '',//审核原因
    infuUserInfo: {},
    userInfo: {},
    orderInfo:{},
    distributerOrderCount: 0,
    projectSource: 'https://github.com/liuxuanqiang/wechat-weapp-mall',
    user_menu1: app.d.imageUrlFix + 'wechatShop/user_menu1.png',
    user_menu2: app.d.imageUrlFix + 'wechatShop/user_menu2.png',
    user_menu3: app.d.imageUrlFix + 'wechatShop/user_menu3.png',
    user_menu4: app.d.imageUrlFix + 'wechatShop/user_menu4.png',
    user_menu5: app.d.imageUrlFix + 'wechatShop/user_menu5.png',
    user_menu6: app.d.imageUrlFix + 'wechatShop/user_menu6.png',
    userListInfo: [ {
        icon: '../../images/iconfont-dingdan.png',
        text: '我的订单',
        isunread: true,
        unreadNum: 2
      }, {
        icon: '../../images/iconfont-card.png',
        text: '我的代金券',
        isunread: false,
        unreadNum: 2
      }, {
        icon: '../../images/iconfont-icontuan.png',
        text: '我的拼团',
        isunread: true,
        unreadNum: 1
      }, {
        icon: '../../images/iconfont-shouhuodizhi.png',
        text: '收货地址管理'
      }, {
        icon: '../../images/iconfont-kefu.png',
        text: '联系客服'
      }, {
        icon: '../../images/iconfont-help.png',
        text: '常见问题'
      }],
       loadingText: '加载中...',
       loadingHidden: false,
       onLoadStatus:{},//刷新是否完成状态
  },
  onLoad: function () {
      var that = this
      //调用应用实例的方法获取全局数据
      app.getUserInfo(function(userInfo){
        //更新数据
        that.setData({
          userInfo:userInfo,
          loadingHidden: true
        })
      });
      that.loadOrderStatus();
      that.loadInfuUserInfo();
      that.queryUserApplyStatus();

  },
  onShow:function(){
    var that = this;
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo,
        loadingHidden: true
      })
    });
    that.loadOrderStatus();
    that.loadInfuUserInfo();
    that.queryUserApplyStatus();
  },

  /**
   * 下拉刷新事件
   */
  onPullDownRefresh: function(){
    var that = this;
    var onLoadStatus = that.data.onLoadStatus;
    onLoadStatus["onLoadNum"] = 0;
    wx.showNavigationBarLoading();
    that.loadOrderStatus();
    that.loadInfuUserInfo();
    that.queryUserApplyStatus();
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      onLoadStatus["onLoadNum"] += 1;
      if (onLoadStatus["onLoadNum"] >= 4) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      //更新数据
      that.setData({
        userInfo: userInfo,
        loadingHidden: true
      })
    });
  },

  /**
   * 跳转申请成为分销商页面
   */
  intentApplyFor: function(){
    var that = this;
    if(that.data.applyForStatus != 1){
      wx.navigateTo({
        url: '../user/distribute',
      })
    }else {
      wx.showToast({
        title: '等待审核中...',
        duration: 2000,
      })
    }
  },

  /**
   * 查询申请成为分销商状态
   */
  queryUserApplyStatus: function () {
    var that = this;
    var onLoadStatus = that.data.onLoadStatus;
    app.request("UserService", "queryApplyStatus", {
      userId: app.d.userId
    }, function(data){
      onLoadStatus["onLoadNum"] += 1;
      if (onLoadStatus["onLoadNum"] >= 4){
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      that.setData({
        onLoadStatus: onLoadStatus,
      });
      if (data.userApplyInfo != null){
        if (data.userApplyInfo.status == null) {
          thats.setData({
            applyForStatus: 0,
          })
        } else if (data.userApplyInfo.status == 0) {
          that.setData({
            applyForStatus: 1,
          });
        } else if (data.userApplyInfo.status == 2) {
          that.setData({
            applyForStatus: 2,
            applyForRemark: data.userApplyInfo.check_remark,
          });
        }
      } 
    });
  },

  loadInfuUserInfo: function(){
    var that = this;
    var onLoadStatus = that.data.onLoadStatus;
    app.request("UserService", "queryUserInfo", {
      userId: app.d.userId
    }, function (data) {
      onLoadStatus["onLoadNum"] += 1;
      if (onLoadStatus["onLoadNum"] >= 4) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      that.setData({
        infuUserInfo: data.userInfo,
        onLoadStatus: onLoadStatus,
      })
      if (data.userInfo.user_type == 1){
        app.request("WxOrderListService", "queryDistributerOrderCount", {
          userId: app.d.userId
        }, function (data) {
          that.setData({
            distributerOrderCount: data.distributerOrderCount
          })
        });
      }
    });
  },
  loadOrderStatus:function(){
    //获取用户订单数据
    var that = this;
    var onLoadStatus = that.data.onLoadStatus;
    app.request("WxOrderListService", "queryOrderCount", {
      userId: app.d.userId,
      status: ["nopay", "paid", "finished","refund"],
    }, function (data) {
      onLoadStatus["onLoadNum"] += 1;
      if (onLoadStatus["onLoadNum"] >= 4) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
      //待付款
      if (data.nopayCount > 99){
        data["nopayCountDes"] = '99+';
      }else{
        data["nopayCountDes"] = data.nopayCount;
      }

      //待收货
      if (data.paidCount > 99){
        data["paidCountDes"] = '99+';
      }else{
        data["paidCountDes"] = data.paidCount;
      }

      //已完成
      if (data.finishedCount > 99){
        data["finishedCountDes"] = '99+';
      }else{
        data["finishedCountDes"] = data.finishedCount;
      }
      
      //退款/售后
      if (data.refundCount > 99){
        data["refundCountDes"] = '99+';
      }else{
        data["refundCountDes"] = data.refundCount;
      }
      that.setData({
        orderInfo: data,
        onLoadStatus: onLoadStatus,
      })
    });

    // wx.request({
    //   url: app.d.ceshiUrl + '/Api/User/getorder',
    //   method:'post',
    //   data: {
    //     userId:app.d.userId,
    //   },
    //   header: {
    //     'Content-Type':  'application/x-www-form-urlencoded'
    //   },
    //   success: function (res) {
    //     //--init data        
    //     var status = res.data.status;
    //     if(status==1){
    //       var orderInfo = res.data.orderInfo;
    //       that.setData({
    //         orderInfo: orderInfo
    //       });
    //     }else{
    //       wx.showToast({
    //         title: '非法操作.',
    //         duration: 2000
    //       });
    //     }
    //   },
    //   error:function(e){
    //     wx.showToast({
    //       title: '网络异常！',
    //       duration: 2000
    //     });
    //   }
    // });
  },
  onShareAppMessage: function () {
      var data = new Date();
      var shared = data.getTime()+"";
      var shareId = shared.substring(8, shared.length) + app.d.userId;
      return {
          title: '大肚商城',
          path: '/pages/index/index?duid=' + app.d.userId + "&shareId=" + shareId,
          success: function(res) {
              app.request("DistributorService", "shareLog", {
                  uid: app.d.userId,
                  shareId: shareId,
              }, function(data){
                  // 分享成功
                  console.log(res);
              })
          },
          fail: function(res) {
              // 分享失败
              console.log(res);
          }
      }
  },
})